package processing;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import util.PathsAndConstants;
import util.Query;

public class QueriesProcessor {

	public List<Query> queriesTokens;
	private List<String> stopWords;

	public QueriesProcessor() {
		this.queriesTokens = new ArrayList<Query>();
		this.stopWords = new ArrayList<String>();
		try {

			String currentLine;

			BufferedReader bReader = new BufferedReader(new FileReader(
					PathsAndConstants.stopWordsPath));

			while ((currentLine = bReader.readLine()) != null) {
				stopWords.add(currentLine);
			}

			bReader.close();

		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public void generateQueriesTokens() {
		List<String> queryWords = null;
		String currentLine = null;
		String currentWord = null;
		Query query;

		try {

			BufferedReader bReader = new BufferedReader(new FileReader(
					PathsAndConstants.queriesPath));
			ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
					PathsAndConstants.queriesTokensPath));

			while ((currentLine = bReader.readLine()) != null) {

				// each line of the file containing the queries is a query
				query = new Query();
				queryWords = Arrays.asList(currentLine.split(" "));
				// the first line is the "name" of the query
				query.setName(queryWords.get(0));

				for (int i = 1; i < queryWords.size(); i++) {

					// remove punctuation and stemm each word in the query
					currentWord = queryWords.get(i);
					currentWord = removePunctuation(currentWord);
					currentWord = Stemmer.normalize(currentWord);

					// add the word in the tokens list if is not empty
					if (stopWords.contains(currentWord)) {
						queryWords.set(i, "");
					} else if (!currentWord.equals("")) {
						query.addToken(currentWord);
					}
				}

				// add the current query to the queries list, which contains the
				// name and the tokens of each query
				this.queriesTokens.add(query);
			}

			// store the list containing all queries and the tokens for each one
			// of them
			out.writeObject(this.queriesTokens);
			bReader.close();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private String removePunctuation(String str) {
		String result = str;
		result = result.replaceAll("[^A-Za-z0-9]", "");
		return result;
	}

}
